/*********************************************************************************
 *      Copyright:  (C) 2022 Zhang Changxing<ZhangChangxingVIP@163.com>
 *                  All rights reserved.
 *
 *       Filename:  serial_init.h
 *    Description:  This file is used for function declarations.
 *                 
 *        Version:  1.0.0(05/1/2022)
 *         Author:  Zhang Changxing <ZhangChangxingVIP@163.com>
 *      ChangeLog:  1, Release initial version on "05/1/2022  01:33:25 AM"
 *                 
 ********************************************************************************/



#ifndef _UART_INIT_H_
#define _UART_INIT_H_

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <termios.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include <iconv.h>
#include <ctype.h>
#include "logger.h"
#include "conf.h"


#define SERIAL_NAME 128




/*--------------------------------------------------------------
 *   description:   Open the serial port File
 *          args:   $ttyusb_ctx:    Serial port property context
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int tty_open(ttyusb_ctx_t *ttyusb_ctx);


/*--------------------------------------------------------------
 *   description:   Closing the serial port File
 *          args:   $ttyusb_ctx:    Serial port property context
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int tty_close(ttyusb_ctx_t *ttyusb_ctx);


/*--------------------------------------------------------------
 *   description:   Example Initialize the serial port properties
 *          args:   $ttyusb_ctx:    Serial port property context
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int tty_init(ttyusb_ctx_t *ttyusb_ctx);


/*--------------------------------------------------------------
 *   description:   Sending data to the serial port
 *          args:   $ttyusb_ctx:    Serial port property context
 *                  $send_buf       Buffer address for data to be sent
 *                  $sbuf_len       The length of the send_buf
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int tty_send(ttyusb_ctx_t *ttyusb_ctx, char *send_buf, int sbuf_len);


/*--------------------------------------------------------------
 *   description:   Receive data from the serial port
 *          args:   $ttyusb_ctx:    Serial port property context
 *                  $recv_buf       Buffer address for data to be receive
 *                  $rbuf_len       The length of the recv_buf
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int tty_recv(ttyusb_ctx_t *ttyusb_ctx, char *recv_buf, int rbuf_len);


/*--------------------------------------------------------------
 *   description:   Get and process the center number
 *          args:   $center_buf:    Receive the processed center number
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int processing_center_number(char *center_buf);


/*--------------------------------------------------------------
 *   description:   Get and process the target number
 *          args:   $center_buf:    Receive the processed center number
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int processing_phone_number(char *phone_buf);


/*--------------------------------------------------------------
 *   description:   Convert UTF8 encoding format to Unicode encoding format
 *          args:   $utf8_buf:      UTF8 Encoding format string
 *                  $unicode_buf    Stores the address of the Unicde coding buffer
 *                  $len            Unicde Indicates the encoded byte length
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int utf8_to_unicode(char *utf8_buf, char *unicode_buf, int *len);


/*--------------------------------------------------------------
 *   description:   Byte stream character stream
 *          args:   $src_buf:       Byte stream buffer address
 *                  $dest_buf:      Character stream buffer address
 *                  $source_len:    Byte stream buffer length
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int hex_to_str(char *dest_buf, char *src_buf, int src_len);


/*--------------------------------------------------------------
 *   description:   The center number, destination number, and 
 *                  short message are packaged into a PDU package
 *          args:   $center_buf:    Address of the center number 
 *                  buffer that has been processed
 *                  $phone_buf:     Target number buffer address 
 *                  already processed
 *                  $sms_buf:       Address of the processed SMS buffer
 *                  $pdu_buf:       Address of the packed PDU buffer
 *                  $cmgs_length    The sum of the length of the target 
 *                  number and the data section
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int pdu_packet(char *center_buf, char *phone_buf, char *sms_buf, char *pdu_buf, int *cmgs_length);



/*---------------------------------------------------------------------------
 *   description:   It is used to send the AT command, receive and judge the return information
 *          args:   $ttyusb_ctx:        Serial port property context
 *                  $at_buf:            A buffer that holds AT instructions
 *                  $expect_buf:        The string you expect to receive
 *                  $return_buf:        The address of the buffer that receives 
 *                  the returned information
 *                  $return_buf_len:   return_buf length
 *  return value:   <0: failure,    =0: ok 
 *---------------------------------------------------------------------------*/
int send_at_cmd(ttyusb_ctx_t *ttyusb_ctx, char *at_buf, const char *expect_buf, char *return_buf, int return_buf_len);


/*---------------------------------------------------------------------------
 *   description:   Check whether the serial port is available
 *          args:   $ttyusb_ctx:        Serial port property context
 *  return value:   <0: failure,    =0: ok 
 *---------------------------------------------------------------------------*/
int check_tyy_ready(ttyusb_ctx_t *ttyusb_ctx);


/*---------------------------------------------------------------------------
 *   description:   Check whether the SIM card exists
 *          args:   $ttyusb_ctx:        Serial port property context
 *  return value:   <0: failure,    =0: ok 
 *---------------------------------------------------------------------------*/
int check_sim_exist(ttyusb_ctx_t *ttyusb_ctx);


/*---------------------------------------------------------------------------
 *   description:   Check whether the SIM has been registered
 *          args:   $ttyusb_ctx:        Serial port property context
 *  return value:   <0: failure,    =0: ok 
 *---------------------------------------------------------------------------*/
int check_sim_login(ttyusb_ctx_t *ttyusb_ctx);


/*---------------------------------------------------------------------------
 *   description:   Check whether the SIM card signal is normal
 *          args:   $ttyusb_ctx:        Serial port property context
 *  return value:   <0: failure,    =0: ok 
 *---------------------------------------------------------------------------*/
int check_sim_signal(ttyusb_ctx_t *ttyusb_ctx);


/*---------------------------------------------------------------------------
 *   description:   Check whether the serial port and SIM are ready
 *          args:   $ttyusb_ctx:        Serial port property context
 *  return value:   <0: failure,    =0: ok 
 *---------------------------------------------------------------------------*/
int check_all_ready(ttyusb_ctx_t *ttyusb_ctx);


/*---------------------------------------------------------------------------
 *   description:   Check whether the serial port and SIM are ready
 *          args:   $ttyusb_ctx:    Serial port property context
 *                  $center_buf     Store the obtained center number
 *  return value:   <0: failure,    =0: ok 
 *---------------------------------------------------------------------------*/
int get_center_number(ttyusb_ctx_t *ttyusb_ctx, char *center_buf);


/*---------------------------------------------------------------------------
 *   description:   The Text format is used to send short messages
 *          args:   $ttyusb_ctx:    Serial port property context
 *                  $phone_buf      Store the obtained target number
 *                  $sms_buf        Information to be sent
 *  return value:   <0: failure,    =0: ok 
 *---------------------------------------------------------------------------*/
int send_text_sms(ttyusb_ctx_t *ttyusb_ctx, char *phone_buf, char *sms_buf);


/*---------------------------------------------------------------------------
 *   description:   Send short messages in PDU format
 *          args:   $ttyusb_ctx:    Serial port property context
 *                  $phone_buf      Store the obtained target number
 *                  $sms_buf        Information to be sent
 *  return value:   <0: failure,    =0: ok 
 *---------------------------------------------------------------------------*/
int send_pdu_sms(ttyusb_ctx_t *ttyusb_ctx, char *phone_buf, char *sms_buf);


/*--------------------------------------------------------------
 *   description:   Character stream byte stream
 *          args:   $source:    Character stream buffer address
 *                  $dest  :    Byte stream buffer address
 *                  $source_len:Character stream buffer length
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int str_to_hex(char *source, char *dest, int source_len);


/*--------------------------------------------------------------
 *   description:   Unicode encoding to UTF8 encoding
 *          args:   $unic_buf:    Unicode encoding buffer address
 *                  $unic_len:    Unicode encoding buffer length
 *                  $utf8_buf:    UTF8 Encoding buffer address
 *                  $utf8_len:    UTF8 Encoding buffer length
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int unicode_to_utf8(char *unic_buf,size_t unic_len,char *utf8_buf,size_t utf8_len);


/*--------------------------------------------------------------
 *   description:   Used for Text format decoding
 *          args:   $source:        Character stream buffer address
 *                  $dest:          Destination buffer address
 *                  $source_len:    Character stream length
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int text_decod(char *source, char *dest, int source_len);


/*--------------------------------------------------------------
 *   description:   This parameter is used to calculate the PDU 
 *                  center number, source number, and data length
 *          args:   $ptr:        Character stream buffer address
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int calculate_len(char *ptr);

/*--------------------------------------------------------------
 *   description:   Get information about a specified number
 *          args:   $ttyusb_ctx:    Serial port property context
 *                  $sms_buf:       The address of the buffer where 
 *                  the returned information is stored
 *                  $sms_buf_len:   sms_buf length
 *                  $target_number: Destination number buffer address
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int get_specified_sms(ttyusb_ctx_t *ttyusb_ctx, char *sms_buf, int sms_buf_len, char *target_number);


/*--------------------------------------------------------------
 *   description:   Get information about a specified number
 *          args:   $ttyusb_ctx:    Serial port property context
 *                  $sms_buf:       The address of the buffer where 
 *                  the returned information is stored
 *                  $sms_buf_len:   sms_buf length
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int get_all_sms(ttyusb_ctx_t *ttyusb_ctx, char *sms_buf, int sms_buf_len);


/*--------------------------------------------------------------
 *   description:   Decoding by 7bit encoding
 *          args:   $source:    Source buffer address
 *                  $dest  :    The target buffer address
 *                  $source_len:Source buffer length
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int decode7bit(const unsigned char* source, char* dest, int source_len);


/*--------------------------------------------------------------
 *   description:   Get information about a specified number
 *          args:   $ttyusb_ctx:    Serial port property context
 *                  $sms_buf:       The address of the buffer where 
 *                  $target_number: Destination number buffer address
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int delete_sms(ttyusb_ctx_t *ttyusb_ctx, char *target_number);


/*--------------------------------------------------------------
 *   description:   Listen to the SMS packet whose index value is 0
 *          args:   $ttyusb_ctx:    Serial port property context
 *                  $sms_buf:       The address of the buffer where 
 *                  $sms_buf_len:   The sms_buf length
 *                  $target_number: Destination number buffer address
 *  return value:   <0: failure,    =0:ok 
 *--------------------------------------------------------------*/
int listen_index0(ttyusb_ctx_t *ttyusb_ctx, char *sms_buf, int sms_buf_len, char *target_number);
#endif